/*eslint-disable*/
// 目录

const catalogue = require("../../model/catelogue/catalogue")
const Catalogue = require("../../model/catelogue/catalogue")
const chapter = require("../../model/catelogue/chapter")

// 引入外部模块 获取当前时间
const { getTime } = require("../../utils/gettime")

// const checkParams = (data) => {
//     if (!data) return { msg: "操作失败", code: "400" }
// }

let attributes = ["id", "title", "creator", "createdate", "updator", "updated"]

// 根据id 查询目录
exports.getCatalogueById = async ({ id }) => {
    try {
        id = Number(id)
        let res = await Catalogue.findOne({
            attributes,
            where: { id },
            include: [{
                // model: Chapter
                association: catalogue.hasMany(chapter, {
                    foreignKey: "catalogueId",
                    targetKey: "id"
                })
                // required: false
            }]
        })
        if (res) return { msg: "获取成功", code: "200", data: res }
        return { msg: "获取失败", code: "400" }
    } catch (err) { return { msg: "操作失败", code: "400", err } }
}

//  查询目录列表
exports.getCatalogueList = async () => {
    let res = await Catalogue.findAll({ attributes })
    if (res) return { msg: "查询成功", code: "200", data: res }
    return { msg: "查询失败", code: "400" }
}
//  查询全部目录
exports.getCatalogueAll = async () => {
    let res = await Catalogue.findAll({
        attributes: ['id', 'title'],
        include:[{
            association: catalogue.hasMany(chapter, {
                foreignKey: "catalogueId",
                targetKey: "id"
            }),
            attributes:['id','title']
        }]
    })
    if (res) return { msg: "查询成功", code: "200", data: res }
    return { msg: "查询失败", code: "400" }
}

// 添加目录
exports.createCatalogue = async ({ title, creator }) => {
    try {
        let isNext = await Catalogue.findOne({ where: { title } })
        if (isNext) return { msg: "重复的目录名称", code: "400" }
        let newData = {}
        newData.title = title
        if (creator) newData.creator = creator
        newData.createdate = getTime()
        let isCreate = await Catalogue.create(newData)
        if (isCreate) return { msg: "添加成功", code: "200" }
        return { msg: "操作失败", code: "400" }
    } catch (err) { return { msg: "操作失败", code: "400", err } }
}

// 编辑目录
exports.editCatalogue = async ({ id, title, updator }) => {
    try {
        let newData = {};
        if (updator) newData.updator = updator
        newData.updated = getTime()
        if (title) {
            let isTitle = await chapter.findOne({ where: { title }, attributes: ["id"] })
            if (isTitle) {
                if (id !== isTitle.id) return { msg: "重复的目录名", code: "400" }
            } else {
                newData.title = title
            }
        }
        let res = await Catalogue.update(newData, { where: { id } })
        if (res) return { msg: "编辑成功", code: "200" }
        return { msg: "操作失败", code: "400" }
    } catch (err) { return { msg: "操作失败", code: "400", err } }
}

// 删除目录
exports.removeCatalogue = async ({ id }) => {
    let isRemove = await Catalogue.destroy({ where: { id } })
    if (isRemove) return { msg: "删除成功", code: "200" }
    return { msg: "删除失败", code: "400" }
}